* This file generates the results in the first five columns of Table 4
* The market reaction to dividend decreases and dividend increases

clear all

* Set your directory path here
local root "D:\Understanding_Bank_Payouts"

capture log close
log using "`root'\Code\Logs\Final_code_RCFS\Table4_dividends.log", replace
display "$S_TIME  $S_DATE"

capture noisily mkdir "`root'\Results"
capture noisily mkdir "`root'\Results\Final_code_RCFS"
cd "`root'\Results\Final_code_RCFS"

// Cumulative abnormal returns for all dividend announcements
use  "`root'\Data\Divdeclare_eventstudy\CARs_quarterly_data.dta"

// Merge dates of earnings announcements from SNL
merge 1:1 instkey quarter using "`root'\Data\Divdeclare_eventstudy\SNL_EAdates_Peter.dta"

gen eagap0 = date - eadate
xtset instkey quarter, quarterly
gen eagapm1 = date - L.eadate
gen eagapp1 = date - F.eadate
sum eagap*
count if abs(eagap0) < 3
count if abs(eagap0) < 2
count if abs(eagap0) < 1

gen divgrQ = D.divpsh
generate div_inc_dec_Q = .
replace div_inc_dec_Q = 0 if divgrQ < 0
replace div_inc_dec_Q = 1 if divgrQ > 0

foreach var of varlist car03_FF4 carm3p3_FF4 carm5p5_FF4 scar03_FF4 scarm3p3_FF4 scarm5p5_FF4 {
	winsor `var', generate(helper) p(0.01)
	drop `var'
	rename helper `var'
	
	sum `var'
	sum `var' if divgrQ < 0
	sum `var' if divgrQ > 0
	ttest `var' == 0 if divgrQ < 0
	ttest `var', by(div_inc_dec_Q) unequal
	tabstat `var' if div_inc_dec_Q == 0, by(year) stats(mean N)
	tabstat `var' if div_inc_dec_Q == 1, by(year) stats(mean N)
	}

// The three different event windows will be saved in three separate files

foreach var in car03_FF4 carm3p3_FF4 carm5p5_FF4 {

	file open CAR_results using "Table4_divdeclare_eventstudy_`var'.txt", write replace

	***************************************************************************
	* Drop all dividend announcements that are on the same day as an earnings announcement
	***************************************************************************
	drop if abs(eagap0) < 1	

	file write CAR_results "The market reaction (`var') to dividend decreases and dividend increases - excluding those that were announced together with earnings" _n(2)

	file write CAR_results "Reaction to dividend decreases - excluding those that were announced together with earnings" _n(2)
	file write CAR_results "Year" _tab "Average CAR" _tab "N" _tab "t statistic" _tab ///
	"p value" _tab "asterisks"  _n

	** Full sample

	summarize `var' if div_inc_dec_Q == 0 & year >= 1995 & year <=2008
	file write CAR_results "Full sample" _tab (r(mean)) _tab (r(N)) _tab	// Show the descriptives on the CAR, and use the Böhmer et al. stat for the t-test
	ttest s`var' == 0 if div_inc_dec_Q == 0 & year >= 1995 & year <=2008
	file write CAR_results (r(t)) _tab (r(p)) 
	local pvalue = r(p)
	if r(p) < 0.01 {
		file write CAR_results "***" _n
		}
	else {
		if r(p) < 0.05 {
				file write CAR_results "**" _n
				}
		else {
			if r(p) < 0.1 {
				file write CAR_results "*" _n
				}	
			else {
				file write CAR_results _n
				}		
			}
		}
	 
	preserve
	keep if year <= 2006
		
	summarize `var' if div_inc_dec_Q == 0 &  year >= 1995 & year <=2008
	file write CAR_results "1995-2006" _tab (r(mean)) _tab (r(N)) _tab	// Show the descriptives on the CAR, and use the Böhmer et al. stat for the t-test
	ttest s`var' == 0 if div_inc_dec_Q == 0 & year >= 1995 & year <=2008
	file write CAR_results (r(t)) _tab (r(p)) 
	local pvalue = r(p)
	if r(p) < 0.01 {
		file write CAR_results "***" _n
		}
	else {
		if r(p) < 0.05 {
				file write CAR_results "**" _n
				}
		else {
			if r(p) < 0.1 {
				file write CAR_results "*" _n
				}	
			else {
				file write CAR_results _n
				}		
			}
		}	

	restore, preserve

	keep if year == 2007 | year == 2008
		
	summarize `var' if div_inc_dec_Q == 0  & year >= 1995 & year <=2008
	file write CAR_results "2007-2008" _tab (r(mean)) _tab (r(N)) _tab	// Show the descriptives on the CAR, and use the Böhmer et al. stat for the t-test
	ttest s`var' == 0 if div_inc_dec_Q == 0  & year >= 1995 & year <=2008
	file write CAR_results (r(t)) _tab (r(p)) 
	local pvalue = r(p)
	if r(p) < 0.01 {
		file write CAR_results "***" _n
		}
	else {
		if r(p) < 0.05 {
				file write CAR_results "**" _n
				}
		else {
			if r(p) < 0.1 {
				file write CAR_results "*" _n
				}	
			else {
				file write CAR_results _n
				}		
			}
		}	

	restore

	file write CAR_results _n(3)	

	file write CAR_results "Reaction to dividend increases - excluding those that were announced together with earnings" _n(2)
	file write CAR_results "Year" _tab "Average CAR" _tab "N" _tab "t statistic" _tab ///
	"p value" _tab "asterisks"  _n

		
	** Full sample

	summarize `var' if div_inc_dec_Q == 1 & year >= 1995 & year <=2008
	file write CAR_results "Full sample" _tab (r(mean)) _tab (r(N)) _tab	// Show the descriptives on the CAR, and use the Böhmer et al. stat for the t-test
	ttest s`var' == 0 if div_inc_dec_Q == 1 & year >= 1995 & year <=2008
	file write CAR_results (r(t)) _tab (r(p)) 
	local pvalue = r(p)
	if r(p) < 0.01 {
		file write CAR_results "***" _n
		}
	else {
		if r(p) < 0.05 {
				file write CAR_results "**" _n
				}
		else {
			if r(p) < 0.1 {
				file write CAR_results "*" _n
				}	
			else {
				file write CAR_results _n
				}		
			}
		}	



	preserve
	keep if year <= 2006
		
	summarize `var' if div_inc_dec_Q == 1 &  year >= 1995 & year <=2008
	file write CAR_results "1995-2006" _tab (r(mean)) _tab (r(N)) _tab	// Show the descriptives on the CAR, and use the Böhmer et al. stat for the t-test
	ttest s`var' == 0 if div_inc_dec_Q == 1 &  year >= 1995 & year <=2008
	file write CAR_results (r(t)) _tab (r(p)) 
	local pvalue = r(p)
	if r(p) < 0.01 {
		file write CAR_results "***" _n
		}
	else {
		if r(p) < 0.05 {
				file write CAR_results "**" _n
				}
		else {
			if r(p) < 0.1 {
				file write CAR_results "*" _n
				}	
			else {
				file write CAR_results _n
				}		
			}
		}	

	restore, preserve

	keep if year == 2007 | year == 2008
		
	summarize `var' if div_inc_dec_Q == 1 &  year >= 1995 & year <=2008
	file write CAR_results "2007-2008" _tab (r(mean)) _tab (r(N)) _tab	// Show the descriptives on the CAR, and use the Böhmer et al. stat for the t-test
	ttest s`var' == 0 if div_inc_dec_Q == 1 &  year >= 1995 & year <=2008
	file write CAR_results (r(t)) _tab (r(p)) 
	local pvalue = r(p)
	if r(p) < 0.01 {
		file write CAR_results "***" _n
		}
	else {
		if r(p) < 0.05 {
				file write CAR_results "**" _n
				}
		else {
			if r(p) < 0.1 {
				file write CAR_results "*" _n
				}	
			else {
				file write CAR_results _n
				}		
			}
		}	

	file close CAR_results
	
	restore 	
	
	}

	
display "$S_TIME  $S_DATE"
log close
clear
exit
